from django.db.models import Q
from django.shortcuts import render, redirect
from django.urls import reverse

from movie.models import MovieInfo
from movie.views import single

# Create your views here.
def search(request):
    """
    设置search函数，实现搜索功能
    :param request:
    :return:
    """

    try:
        datalist=MovieInfo.objects.filter(movie_name__icontains=request.GET['Search'])
        #确认提交过来的需要搜索的内容
    except Exception:
        try:
            datalist=MovieInfo.objects.filter(movie_name__icontains=request.GET['email'])
            #同上，因搜索框存在两处
        except Exception:
            return_url = request.environ.get('HTTP_REFERER', reverse('index'))
            return redirect(return_url)

    if datalist:
        #如果存在结果，即返回单页详情页面
        pk=datalist[0].pk
        return single(request,pk)
    else:
        #不存在结果，返回搜索页面或者首页
        return_url = request.environ.get('HTTP_REFERER', reverse('index'))
        return redirect(return_url)


